package com.wzl.backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzl.backend.common.ResponseCode;
import com.wzl.backend.common.Result;
import com.wzl.backend.pojo.Topic;
import com.wzl.backend.service.TopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/topic")
@Api(tags = "学习话题接口")
@CrossOrigin("*")
public class TopicController {

    @Autowired
    TopicService topicService;

    @GetMapping("{id}")
    @ApiOperation("根据用户id查询话题")
    public Result getById(@PathVariable String id) {
        QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id" , id);
        return Result.ok(topicService.list(queryWrapper));
    }

    @PostMapping("insert")
    @ApiOperation("添加一个话题")
    public Result insert(Topic topic) {
        boolean insert = topicService.insert(topic);
        if (insert) {
            return Result.ok();
        } else {
            return Result.fail(ResponseCode.BAD_REQUEST);
        }
    }

    @PostMapping("delete/{id}")
    @ApiOperation("删除一个话题")
    public Result delete(@PathVariable String id) {
        return Result.ok(topicService.removeById(id));
    }

    @GetMapping("getAll")
    @ApiOperation("获取所有话题")
    public Result getAll() {
        return Result.ok(topicService.getAll());
    }

}
